home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / RUMORS.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  8KB  |  302 lines

  1. /*    SCCS Id: @(#)rumors.c    3.0    89/02/08
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4. /* hack.rumors.c - version 1.0.3 */
  5.  
  6. #include    "hack.h"        /* for RUMORFILE and BSD (index) */
  7.  
  8. /* Rumors has been entirely rewritten to speed up the access.  This is
  9.  * essential when working from floppies.  Using fseek() the way that's done
  10.  * here means rumors following longer rumors are output more often than those
  11.  * following shorter rumors.  Also, you may see the same rumor more than once
  12.  * in a particular game (although the odds are highly against it), but
  13.  * this also happens with real fortune cookies.  Besides, a person can
  14.  * just read the rumor file if they desire.  -dgk
  15.  */
  16.  
  17. /* The rumors file consists of a long giving the number of bytes of useful/true
  18.  * rumors, followed by the true rumors (one per line), followed by the useless/
  19.  * false/misleading/cute rumors (one per line).
  20.  */
  21.  
  22. /* The oracle file consists of a number of multiple-line records, separated
  23.  * (but not terminated) by "-----" lines.
  24.  */
  25. static void NDECL(init_rumors);
  26. #ifdef ORACLE
  27. static void NDECL(outoracle);
  28. #endif
  29. long first_rumor = sizeof(long);
  30. long true_rumor_size, false_rumor_size, end_rumor_file;
  31. #ifdef ORACLE
  32. long oracle_size;
  33. #endif
  34.  
  35. static void
  36. init_rumors()
  37. {
  38.     register FILE *fp;
  39.  
  40. #ifdef OS2_CODEVIEW
  41.     {
  42.     char tmp[PATHLEN];
  43.  
  44.     Strcpy(tmp,hackdir);
  45.     append_slash(tmp);
  46.     Strcat(tmp,RUMORFILE);
  47.     if(fp = fopen(tmp, "r")) {
  48. #else
  49. # ifdef MACOS
  50.     if(!(fp = fopen(RUMORFILE, "r")))
  51.         fp = openFile(RUMORFILE, "r");
  52.     if (fp) {
  53. # else
  54.     if(fp = fopen(RUMORFILE, "r")) {
  55. # endif
  56. #endif
  57.         (void) fread((genericptr_t)&true_rumor_size,sizeof(long),1,fp);
  58.         (void) fseek(fp, 0L, 2);
  59.         end_rumor_file = ftell(fp);
  60.         false_rumor_size = (end_rumor_file-sizeof(long)) - true_rumor_size;
  61.         (void) fclose(fp);
  62.     } else {
  63.         pline("Can't open rumors file!");
  64.         end_rumor_file = -1;    /* don't try to open it again */
  65.     }
  66. #ifdef OS2_CODEVIEW
  67.     }
  68. #endif
  69. #ifdef ORACLE
  70. #ifdef OS2_CODEVIEW
  71.     {
  72.     char tmp[PATHLEN];
  73.  
  74.     Strcpy(tmp,hackdir);
  75.     append_slash(tmp);
  76.     Strcat(tmp,ORACLEFILE);
  77.     if(fp = fopen(tmp, "r")) {
  78. #else
  79. # ifdef MACOS
  80.     if(!(fp = fopen(ORACLEFILE, "r")))
  81.         fp = openFile(ORACLEFILE, "r");
  82.     if (fp) {
  83. # else
  84.     if(fp = fopen(ORACLEFILE, "r")) {
  85. # endif
  86. #endif
  87.         (void) fseek(fp, 0L, 2);
  88.         oracle_size = ftell(fp);
  89.         (void) fclose(fp);
  90.     } else {
  91.         pline("Can't open oracles file!");
  92.         oracle_size = -1;    /* don't try to open it again */
  93.     }
  94. #ifdef OS2_CODEVIEW
  95.     }
  96. #endif
  97. #endif
  98. }
  99.  
  100.  
  101. void
  102. outrumor(truth,cookie)
  103. int truth; /* 1=true, -1=false, 0=either */
  104. boolean cookie;
  105. {
  106.     static const char fortune_msg[] =
  107.         "This cookie has a scrap of paper inside.";
  108.     char    line[COLNO];
  109.     char    *endp;
  110.     FILE    *rumors;
  111.     long tidbit, beginning;
  112.  
  113.     if (cookie && Blind) {
  114.         pline(fortune_msg);
  115.         pline("What a pity that you cannot read it!");
  116.         return;
  117.     }
  118.     if (end_rumor_file < 0) /* We couldn't open RUMORFILE */
  119.         return;
  120. #ifdef OS2_CODEVIEW
  121.     {
  122.     char tmp[PATHLEN];
  123.  
  124.     Strcpy(tmp,hackdir);
  125.     append_slash(tmp);
  126.     Strcat(tmp,RUMORFILE);
  127.     if(rumors = fopen(tmp, "r")) {
  128. #else
  129. # ifdef MACOS
  130.     if(!(rumors = fopen(RUMORFILE, "r")))
  131.         rumors = openFile(RUMORFILE, "r");
  132.     if (rumors) {
  133. # else
  134.     if(rumors = fopen(RUMORFILE, "r")) {
  135. # endif
  136. #endif
  137.         if (!end_rumor_file) {    /* if this is the first outrumor() */
  138.             init_rumors();
  139.         }
  140.         if (!truth) truth = (rn2(100) >= 50 ? 1 : -1);
  141.         /* otherwise, 50% chance of being true */
  142.         switch(truth) {
  143.             case 1: beginning = first_rumor;
  144.             tidbit = Rand() % true_rumor_size;
  145.             break;
  146.             case -1: beginning = first_rumor + true_rumor_size;
  147.             tidbit = true_rumor_size + Rand() % false_rumor_size;
  148.             break;
  149.             default:
  150.             impossible("strange truth value for rumor");
  151.             tidbit = 0; beginning = first_rumor;
  152.             break;
  153.         }
  154.         (void) fseek(rumors, first_rumor + tidbit, 0);
  155.         (void) fgets(line, COLNO, rumors);
  156.         if (!fgets(line, COLNO, rumors) || (truth == 1 &&
  157.             (ftell(rumors) > true_rumor_size + sizeof(long)))) {
  158.             /* reached end of rumors -- go back to beginning */
  159.             (void) fseek(rumors, beginning, 0);
  160.             (void) fgets(line, COLNO, rumors);
  161.         }
  162.         if (endp = index(line, '\n')) *endp = 0;
  163.         if (cookie) {
  164.             pline(fortune_msg);
  165.             pline("It reads:");
  166.         } else pline("Tidbit of information #%ld: ",tidbit);
  167.         pline(line);
  168.         (void) fclose(rumors);
  169.     } else {
  170.         pline("Can't open rumors file!");
  171.         end_rumor_file = -1;    /* don't try to open it again */
  172.     }
  173. #ifdef OS2_CODEVIEW
  174.     }
  175. #endif
  176. }
  177.  
  178. #ifdef ORACLE
  179. static void
  180. outoracle()
  181. {
  182.     char    line[COLNO];
  183.     char    *endp;
  184.     FILE    *oracles;
  185.  
  186.     if (oracle_size < 0)    /* We couldn't open ORACLEFILE */
  187.         return;
  188. #ifdef OS2_CODEVIEW
  189.     {
  190.     char tmp[PATHLEN];
  191.  
  192.     Strcpy(tmp,hackdir);
  193.     append_slash(tmp);
  194.     Strcat(tmp,ORACLEFILE);
  195.     if(oracles = fopen(tmp, "r")) {
  196. #else
  197. # ifdef MACOS
  198.     if(!(oracles = fopen(ORACLEFILE, "r")))
  199.         oracles = openFile(ORACLEFILE, "r");
  200.     if (oracles) {
  201. # else
  202.     if(oracles = fopen(ORACLEFILE, "r")) {
  203. # endif
  204. #endif
  205.         if (!oracle_size) {    /* if this is the first outrumor() */
  206.             init_rumors();
  207.         }
  208.         (void) fseek(oracles, Rand() % oracle_size, 0);
  209.         (void) fgets(line, COLNO, oracles);
  210.         while (1)
  211.             if (!fgets(line, COLNO, oracles)) {
  212.             /* reached end of oracle info -- go back to beginning */
  213.             (void) fseek(oracles, 0L, 0);
  214.             break;
  215.             } else if (!strncmp(line,"-----",5)) {
  216.             /* found end of an oracle proclamation */
  217.             break;
  218.             }
  219.         pline("The Oracle meditates for a moment and then intones: ");
  220.         cornline(0,NULL);
  221.         while (fgets(line, COLNO, oracles) && strncmp(line,"-----",5)) {
  222.             if (endp = index(line, '\n')) *endp = 0;
  223.             cornline(1,line);
  224.         }
  225.         cornline(2,"");
  226.         (void) fclose(oracles);
  227.     } else {
  228.         pline("Can't open oracles file!");
  229.         oracle_size = -1;    /* don't try to open it again */
  230.     }
  231. #ifdef OS2_CODEVIEW
  232.     }
  233. #endif
  234. }
  235.  
  236. int
  237. doconsult(oracl)
  238. register struct monst *oracl;
  239. {
  240.     register char ans;
  241.  
  242.     multi = 0;
  243.     (void) inshop();
  244.  
  245.     if(!oracl) {
  246.         pline("There is no one here to consult.");
  247.         return(0);
  248.     }
  249.     if(!oracl->mpeaceful) {
  250.         pline("The Oracle is in no mood for consultations.");
  251.         return(0);
  252.     } else {
  253.         if(!u.ugold) {
  254.             You("have no money.");
  255.             return(0);
  256.         }
  257.         pline("\"Wilt thou settle for a minor consultation?\"  (50 zorkmids) ");
  258.         ans = ynq();
  259.         if(ans == 'y') {
  260.             if(u.ugold < 50) {
  261.                 You("don't even have enough money for that!");
  262.                 return(0);
  263.             }
  264.             u.ugold -= 50;
  265.             oracl->mgold += 50;
  266.             flags.botl = 1;
  267.             outrumor(1, FALSE);
  268.             return(1);
  269.         } else if(ans == 'q') return(0);
  270.         else {
  271.             pline("\"Then dost thou desire a major one?\"  (1000 zorkmids) ");
  272.             if (yn() != 'y') return(0);
  273.         }
  274.         if(u.ugold < 1000) {
  275.         pline("The Oracle scornfully takes all your money and says:");
  276. cornline(0,NULL);
  277. cornline(1,"\"...it is rather disconcerting to be confronted with the");
  278. cornline(1,"following theorem from [Baker, Gill, and Solovay, 1975].");
  279. cornline(1,"");
  280. cornline(1,"Theorem 7.18  There exist recursive languages A and B such that");
  281. cornline(1,"  (1)  P(A) == NP(A), and");
  282. cornline(1,"  (2)  P(B) != NP(B)");
  283. cornline(1,"");
  284. cornline(1,"This provides impressive evidence that the techniques that are");
  285. cornline(1,"currently available will not suffice for proving that P != NP or");
  286. cornline(1,"that P == NP.\"  [Garey and Johnson, p. 185.]");
  287. cornline(2,"");
  288.             oracl->mgold += u.ugold;
  289.             u.ugold = 0;
  290.             flags.botl = 1;
  291.             return(1);
  292.         }
  293.         u.ugold -= 1000;
  294.         oracl->mgold += 1000;
  295.         flags.botl = 1;
  296.         outoracle();
  297.         return(1);
  298.     }
  299. }
  300.  
  301. #endif
  302.